package aries.utils;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class Util implements ClipboardOwner {

    /**
     * Memory parameter.
     */
    public static final byte USED_MEMORY = 0;
    /**
     * Memory parameter.
     */
    public static final byte FREE_MEMORY = 1;
    /**
     * Memory parameter.
     */
    public static final byte TOTAL_MEMORY = 2;
    /**
     * Memory parameter.
     */
    public static final byte MAX_MEMORY = 3;
    /**
     * Get info parameter.
     */
    public static final byte HOME_DIRECTORY = 0;
    /**
     * Get info parameter.
     */
    public static final byte CURRENT_WORKING_DIRECTORY = 1;
    /**
     * Get info parameter.
     */
    public static final byte USER_NAME = 2;
    /**
     * Get info parameter.
     */
    public static final byte OS_VERSION = 3;
    /**
     * Get info parameter.
     */
    public static final byte OS_ARCHITECTURE = 4;
    /**
     * Get info parameter.
     */
    public static final byte OS_NAME = 5;
    /**
     * Get info parameter.
     */
    public static final byte JAVA_HOME = 6;
    /**
     * Get info parameter.
     */
    public static final byte JAVA_VERSION = 7;
    /**
     * Get info parameter.
     */
    public static final byte JAVA_VENDOR = 8;
    /**
     * Get info parameter.
     */
    public static final byte JAVA_CLASS_PATH = 9;
    /**
     * Get info parameter.
     */
    public static final byte JAVA_CLASS_VERSION = 10;
    /**
     * Get info parameter.
     */
    public static final byte JRE_VERSION = 11;
    /**
     * Get info parameter.
     */
    public static final byte JRE_VENDOR = 12;
    /**
     * Get info parameter.
     */
    public static final byte JRE_NAME = 13;
    /**
     * Get info parameter.
     */
    public static final byte JAVA_LIBRARY_PATH = 14;
    /**
     * Get info parameter.
     */
    public static final byte JAVA_TEMP_PATH = 15;
    /**
     * Get info parameter.
     */
    public static final byte JAVA_EXTENSION_PATH = 16;
    /**
     * Get info parameter.
     */
    public static final byte JAVA_COMPILER = 17;

    /**
     * Remove first index of an Array.
     *
     * @param text
     * @return array without first index
     */
    public static String[] removeFirstIndex(String[] text) {
        String[] temp = new String[text.length - 1];
        int j = 1;
        for (int i = 0; i < text.length - 1; i++) {
            temp[i] = text[j];
            j++;
        }
        return temp;
    }

    /**
     * Get current executed method name.
     * 
     * @return String
     */
    public static String getCurrentMethod() {
        return Thread.currentThread().getStackTrace()[1].getMethodName();
    }

    /**
     * Get memory information. 
     * The result is in MB.
     * 
     * @param type
     * @return long
     */
    public static long getMemory(byte type) {
        int mb = 1024 * 1024;
        Runtime runtime = Runtime.getRuntime();
        long memory = 0;
        switch (type) {
            case 0:
                memory = ((runtime.totalMemory() - runtime.freeMemory()) / mb);
                break;
            case 1:
                memory = (runtime.freeMemory() / mb);
                break;
            case 2:
                memory = (runtime.totalMemory() / mb);
                break;
            case 3:
                memory = (runtime.maxMemory() / mb);
                break;
        }
        return memory;
    }

    /**
     * Stopping your current process.
     */
    public static void stopProcess() {
        Runtime runtime = Runtime.getRuntime();
        runtime.halt(0);
    }

    /**
     * Run a new application. Without using Task Manager.
     */
    public static void runApplication(String application) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        runtime.exec(application);
    }

    /**
     * Run new applications. Without using Task Manager.
     */
    public static void runApplication(String[] application) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        runtime.exec(application);
    }

    /**
     * Get number of processors.
     */
    public static int getProcessor() {
        Runtime runtime = Runtime.getRuntime();
        return runtime.availableProcessors();
    }

    /**
     * Check if your OS is Windows or not.
     * 
     * @return Boolean
     */
    public static Boolean isWindows() {
        return (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0);
    }

    /**
     * Check if your OS is Mac or not.
     * 
     * @return Boolean
     */
    public static Boolean isMac() {
        return (System.getProperty("os.name").toLowerCase().indexOf("mac") >= 0);
    }

    /**
     * Check if your OS is a Unix or Linux or not.
     * 
     * @return Boolean
     */
    public static Boolean isUnix() {
        return (System.getProperty("os.name").toLowerCase().indexOf("nix") >= 0
                || System.getProperty("os.name").toLowerCase().indexOf("nux") >= 0);
    }

    /**
     * Get information about your OS, installed Java version and etc.
     * 
     * @return String
     */
    public static String getInfo(byte info) {
        String theInfo = "";
        switch (info) {
            case 0:
                theInfo = System.getProperty("user.home");
                break;
            case 1:
                theInfo = System.getProperty("user.dir");
                break;
            case 2:
                theInfo = System.getProperty("user.name");
                break;
            case 3:
                theInfo = System.getProperty("os.version");
                break;
            case 4:
                theInfo = System.getProperty("os.arch");
                break;
            case 5:
                theInfo = System.getProperty("os.name");
                break;
            case 6:
                theInfo = System.getProperty("java.home");
                break;
            case 7:
                theInfo = System.getProperty("java.version");
                break;
            case 8:
                theInfo = System.getProperty("java.vendor");
                break;
            case 9:
                theInfo = System.getProperty("java.class.path");
                break;
            case 10:
                theInfo = System.getProperty("java.class.version");
                break;
            case 11:
                theInfo = System.getProperty("java.specification.version");
                break;
            case 12:
                theInfo = System.getProperty("java.specification.vendor");
                break;
            case 13:
                theInfo = System.getProperty("java.specification.name");
                break;
            case 14:
                theInfo = System.getProperty("java.library.path");
                break;
            case 15:
                theInfo = System.getProperty("java.io.tmpdir");
                break;
            case 16:
                theInfo = System.getProperty("java.ext.dirs");
                break;
            case 17:
                theInfo = System.getProperty("java.compiler");
                break;
        }
        return theInfo;
    }

    /**
     * Set clipboard contents.
     * 
     * @param aString 
     */
    public void setClipboard(String value) {
        StringSelection stringSelection = new StringSelection(value);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, this);
    }

    /**
     * Get clipboard contents.
     * 
     * @return String
     * @throws UnsupportedFlavorException
     * @throws IOException 
     */
    public String getClipboardContents() throws UnsupportedFlavorException, IOException {
        String result = "";
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable contents = clipboard.getContents(null);
        boolean hasTransferableText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
        if (hasTransferableText) {
            result = (String) contents.getTransferData(DataFlavor.stringFlavor);
        }
        return result;
    }

    /**
     * Lost ownership.
     * 
     * @param clpbrd
     * @param t 
     */
    public void lostOwnership(Clipboard clpbrd, Transferable t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    /**
     * Convert array object into string object.
     * 
     * @param object
     * @return String[]
     */
    public static String[] ObjectToArray(Object[] object) {
        String[] theString = new String[object.length];
        for (int i=0; i<object.length; i++) {
            theString[i] = object[i].toString();
        }
        return theString;
    }
}